package testBusiness;

import com.alibaba.fastjson.JSONObject;
import models.Http;
import models.MyConfig;
import models.MySleep;
import org.openqa.selenium.WebDriver;

import java.util.ArrayList;
import java.util.Objects;

/**
 * Created by yangxianming on 2018/12/14.
 * ArrayList 来循环处理发来的操作请求,处理uitest
 */
public class No1Factory extends Thread{
    private ArrayList<JSONObject> taskList = new ArrayList<JSONObject>();
    private WebDriver wb;
    private MyConfig myConfig;
    private Boolean needSaveReport = false;

    public WebDriver getWb() {
        return wb;
    }

    public void setWb(WebDriver wb) {
        this.wb = wb;
    }

    public void setMyConfig(MyConfig myConfig) {
        this.myConfig = myConfig;
        String strneedSaveReport = this.myConfig.getPropertiesConfig().getProperties().getProperty("needSaveReport");
        needSaveReport = strneedSaveReport.equals("true");
        System.out.println("strneedSaveReport----------" + needSaveReport);
    }

    public void inputFactory(JSONObject jo){
        this.taskList.add(jo);
    }

    public int getListSize(){
        return taskList.size();
    }

    @Override
    public void run() {
        ActionScheduler actionScheduler = new ActionScheduler();
        actionScheduler.setActionScheduler(actionScheduler);
        Http http = new Http();
        final int closeDriveTimeFinal = 60;     //关闭浏览器倒计时 60 X 1000ms
        int closeDriveTime = closeDriveTimeFinal;
        while (true){
            MySleep.sleep(1000);
            if (taskList.size() <= 0){
                if (closeDriveTime-- < 0 && !Objects.equals(null, wb)){
                    //一段时间后关闭浏览器
                    wb.close();
                    wb.quit();
                    wb = null;
                    System.out.println("It's no task after long time, close web-driver!!!!!");
                    //重置倒计时
                    closeDriveTime = closeDriveTimeFinal;
                }
                continue;
            }
            //有任务倒计时重置
            closeDriveTime = closeDriveTimeFinal;


            //把data数组传给ActionScheduler做处理
            System.out.println("taskList size:" + taskList.size());
            JSONObject r = actionScheduler.doAction(myConfig, taskList.get(0).getJSONArray("data"));
            if (Objects.equals(r.getString("result"), "")){
                System.out.println("result is \"\", don't send report!!");
                taskList.remove(0);
                continue;
            }
            String caseId = taskList.get(0).getString("caseId");
            String callBackIp = taskList.get(0).getString("callBackIp");
            //补上http协议标识
            if (!Objects.equals(callBackIp.substring(0,4), "http")){
                callBackIp = "http://" + callBackIp;
            }
            String url = callBackIp + "/caseManager/saveReport";
            System.out.println("callbackIp:" + url);
            r.put("caseId", caseId);
            r.put("type", "1");
            //输出测试结果
            if (needSaveReport){
                System.out.println("send result, " + r);
                http.sendRequest("post", null, url, r.toJSONString());
            }else {
                System.out.println("Do dot send result, needSaveReport is false");
            }


            taskList.remove(0);
        }
    }
}
